home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / rmold / rmold.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-12  |  4.1 KB  |  167 lines

  1. /* 
  2.  * rmold.c --
  3.  *
  4.  *    Program to remove files older than a certain number of days.
  5.  *
  6.  * Copyright 1990 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/admin/rmold/RCS/rmold.c,v 1.2 91/03/12 13:52:57 kupfer Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #ifndef ds3100
  21. #ifndef lint
  22. #define _HAS_PROTOTYPES
  23. #define _HAS_VOIDPTR
  24. #endif
  25. #endif
  26.  
  27. #include <sprite.h>
  28. #include <errno.h>
  29. #include <option.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <sys/time.h>
  36. #include <unistd.h>
  37.  
  38. /*
  39.  * Command-line options:
  40.  */
  41.  
  42. int delete = 1;
  43. int mod = 0;        /* 1 means use modify time for deletion, 0 means
  44.              * use access time. */
  45. char *timesDirectory = ".";    /* directory to get mod/access times from */
  46.  
  47. Option optionArray[] = {
  48.     OPT_DOC,    (char *) NULL,    (char *) NULL,
  49.         "This program deletes files that haven't been modifed recently.\n Synopsis: \"rmold [-print] numDays file file ...\"\n Command-line switches are:",
  50.     OPT_TRUE,    "mod",        (char *) &mod,
  51.         "Use modify time instead of access time",
  52.     OPT_FALSE,    "print",    (char *) &delete,
  53.         "Don't delete, just print file names that would be deleted",
  54.     OPT_STRING, "timesFrom",    (char *)×Directory,
  55.         "Name the directory to determine the access or modify time.",
  56. };
  57.  
  58. static char *AllocSpace _ARGS_ ((int numNames, char *namesArray[]));
  59.  
  60.  
  61. int
  62. main(argc, argv)
  63.     int argc;
  64.     char **argv;
  65. {
  66.     int i;
  67.     u_long cutoffTime;
  68.     struct timeval now;
  69.     struct timezone dummy;
  70.     char *end;
  71.     struct stat buf;
  72.     char *fileName;        /* file name for getting access/mod time */
  73.  
  74.     /*
  75.      * Check arguments and compute the cutoff time.
  76.      */
  77.  
  78.     argc = Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), 0);
  79.     if (argc < 3) {
  80.     fprintf(stderr, "Usage: %s numDays file file ...\n", argv[0]);
  81.     exit(1);
  82.     }
  83.     i = strtoul(argv[1], &end, 0);
  84.     if (end == argv[1]) {
  85.     badArg:
  86.     fprintf(stderr, "Bad \"numDays\" argument \"%s\"\n", argv[1]);
  87.     exit(1);
  88.     }
  89.     i *= 24*3600;
  90.     gettimeofday(&now, &dummy);
  91.     if (i > now.tv_sec) {
  92.     goto badArg;
  93.     }
  94.     cutoffTime = now.tv_sec - i;
  95.  
  96.     fileName = AllocSpace(argc-2, argv+2);
  97.  
  98.     /*
  99.      * Scan through all of the files and delete any that haven't been
  100.      * accessed since the cutoff time.
  101.      */
  102.  
  103.     for (i = 2; i < argc; i++) {
  104.     strcpy(fileName, timesDirectory);
  105.     strcat(fileName, "/");
  106.     strcat(fileName, argv[i]);
  107.     if (stat(fileName, &buf) != 0) {
  108.         fprintf(stderr, "Couldn't stat \"%s\": %s\n",
  109.             fileName, strerror(errno));
  110.         continue;
  111.     }
  112.     if (mod) {
  113.         if (buf.st_mtime > cutoffTime) {
  114.         continue;
  115.         }
  116.     } else {
  117.         if (buf.st_atime > cutoffTime) {
  118.         continue;
  119.         }
  120.     }
  121.     if (delete && (unlink(argv[i]) != 0)) {
  122.         fprintf(stderr, "Couldn't remove \"%s\": %s\n",
  123.             argv[i], strerror(errno));
  124.         continue;
  125.     }
  126.     printf("%s\n", argv[i]);
  127.     }
  128.  
  129.     exit(0);
  130.     return 0;            /* suppress -Wall complaint */
  131. }
  132.  
  133.  
  134. /*
  135.  *----------------------------------------------------------------------
  136.  *
  137.  * AllocSpace --
  138.  *
  139.  *    Allocate a character buffer large enough for all the file names.
  140.  *
  141.  * Results:
  142.  *    A character array large enough to hold the concatentation of 
  143.  *    the search directory, "/", and any of the arguments.
  144.  *
  145.  * Side effects:
  146.  *    None.
  147.  *
  148.  *----------------------------------------------------------------------
  149.  */
  150.  
  151. static char *
  152. AllocSpace(numNames, namesArray)
  153.     int numNames;
  154.     char *namesArray[];        /* list of file names */
  155. {
  156.     int i;            /* index into argv */
  157.     int maxLength = 0;        /* longest length in namesArray */
  158.  
  159.     for (i = 0; i < numNames; i++) {
  160.     if (strlen(namesArray[i]) > maxLength) {
  161.         maxLength = strlen(namesArray[i]);
  162.     }
  163.     }
  164.  
  165.     return malloc(strlen(timesDirectory) + strlen("/") + maxLength + 1);
  166. }
  167.